#include <sys/socket.h>
#include <netinet/in.h>

#include <err.h>
#include <stdlib.h>

#include <pthread.h>

#include "thread_data.h"

extern void *process_client(void *arg);

void
loop_accept(int socket)
{
	struct sockaddr_storage addr;
	socklen_t slen;
	int client;
	struct thread_data *pd;
	pthread_t tid;

	for (;;) {
		/* important */
		slen = sizeof(addr);
		if ((client = accept(socket, (struct sockaddr *)&addr,
					&slen)) == -1) {
			warn("accept");
			continue;
		}

		pd = malloc(sizeof(*pd));
		if (pd == NULL) {
			warn("malloc");
			continue;
		}

		pd->fd = client;
		pd->addr = addr;

		if (pthread_create(&tid, NULL, process_client, pd))
			warn("pthread_create");
	}
}
